package marvin.client.rtp;

import marvin.client.player.MP3Player;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import marvin.client.core.SubscriptionClient;

public class StreamReceiver {
	private String radioURL;
	private URL mrl;
	private MP3Player player = null;
	private File file;
	private SubscriptionClient client;

	public StreamReceiver(File file, SubscriptionClient subscriptionClient){
		this.file = file;
		client = subscriptionClient;
	}
	
	public StreamReceiver(String radioURL){
		this.radioURL = radioURL;
	}
	
	public void connect(){
	}
	
	public void connectToWebRadio(){
		try {
			mrl = new URL(radioURL);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	public void connectFile(){
		try {
			mrl = new URL("file:////"+file.getAbsolutePath());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	public void createPlayer(){
		player = new MP3Player(mrl);
		player.play();
		player.addMP3PlayerListener(new PlayerControllerListener(this));
	}
	
	public void disconnect() {
		player.stop();
	}

	public void stopPlaying() {
		player.stop();	
	}
	public void suspendPlaying() {
		player.pause();//stop();		
	}
	public void resumePlaying() {
		player.play();//start();		
	}

	public void sendEndOfMedia() {
		client.sendEndOfMedia();
	}
	
	public void sendIncorrectEndOfMedia() {
		System.err.println("Incorrect end of media");
		client.sendEndOfMedia();
	}
}